/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.cancel;

import cn.hutool.core.collection.CollUtil;
import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.service.pay.api.PayServiceApi;
import com.lxk.service.pay.param.PayRefundParam;
import com.lxk.service.setting.api.SettingServiceApi;
import com.lxk.service.setting.response.ChannelWXPayDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;

import java.util.List;

import static com.lxk.oms.flow.cancel.FlowCancelOrderNodeConst.WX_ROLLBACK_CANCEL_ORDER_NODE;

/**
 * 微信退款
 *
 * @author heyi
 * 2024/9/29
 */
@AllArgsConstructor
@LiteflowComponent(value = WX_ROLLBACK_CANCEL_ORDER_NODE)
public class WxPayRollBackNode extends NodeComponent {
    private final PayServiceApi payServiceApi;
    private final SettingServiceApi settingServiceApi;
    @Override
    public void process() throws Exception {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        String tenantId = omsOrderVo.getTenantId();
        PayRefundParam param = new PayRefundParam();
        param.setRefundFee(omsOrderVo.getActReceiveAmount());
        param.setOutTradeNo(omsOrderVo.getOutTradeNo());
        param.setUserId(omsOrderVo.getUserId());
        param.setRefundReason(contextBean.getCancelReason());
        param.setTradeType(omsOrderVo.getPayType());
        param.setOutRefundNo(contextBean.getOutRefundNo());
        param.setTotalFee(omsOrderVo.getActPayPrice());
        List<ChannelWXPayDto> channelWXPay= settingServiceApi.wxPayConfigByTenantId(tenantId);
        if (CollUtil.isNotEmpty(channelWXPay)) {
            param.setAppId(channelWXPay.get(0).getAppId());
        }
        payServiceApi.refund(param);

    }

    @Override
    public void onSuccess() throws Exception {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        OrderOptionLogEvent optionLogEvent = new OrderOptionLogEvent();
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        optionLogEvent.setOrderSn(omsOrderVo.getOrderNo());
        optionLogEvent.setUserId(contextBean.getUserId());
        optionLogEvent.setOptionUserId(contextBean.getOptionUserId());
        optionLogEvent.setOptionUserType(contextBean.getOptionUserType());
        optionLogEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.WX_ROLL_BACK_ORDER.formatContent(omsOrderVo.getActReceiveAmount().toString()));
        optionLogEvent.setUserType(contextBean.getUserType());
        optionLogEvent.setMerId(omsOrderVo.getMerId());
        optionLogEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
        optionLogEvent.setTenantId(contextBean.getTenantId());
        EventHelper.publishEvent(optionLogEvent);
    }

    @Override
    public boolean isAccess() {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        Boolean success = contextBean.getSuccess();
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        String payType = omsOrderVo.getPayType();
        boolean equals = OrderEnums.PayType.WX_PAY.contains(payType);
        return success && equals;
    }
}
